﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class MONHOC_Form : Form
    {
        MONHOC monhoc = new MONHOC();
        MONHOC_Data monhoc_Data = new MONHOC_Data();
        public MONHOC_Form()
        {
            InitializeComponent();
        }


        private void MONHOC_Form_Load(object sender, EventArgs e)
        {
            txtMaMonHoc.Text = "MH";
            txtTenMonHoc.Text = "";
            nmTCLT.Value = 0;
            nmTCTH.Value = 0;
        }


        private void btnHienThi_Click(object sender, EventArgs e)
        {
            txtMaMonHoc.Text = "MH";
            txtTenMonHoc.Text = "";
            nmTCLT.Value = 0;
            nmTCTH.Value = 0;
            monhoc.hienthi_MonHoc(dtgMonHoc);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaMonHoc.Text == "")
            {
                txtMaMonHoc.Focus();
                MessageBox.Show("Ban chua dien Ma Mon Hoc");
                return;
            }
            else if (txtTenMonHoc.Text == "")
            {
                txtTenMonHoc.Focus();
                MessageBox.Show("Ban chua dien Ten Mon Hoc");
                return;
            }
            else if (nmTCLT.Value <= 0)
            {
                MessageBox.Show("Tin Chi Ly Thuyet khong hop le");
                return;
            }
            else if (nmTCTH.Value <= 0)
            {
                MessageBox.Show("Tin Chi Thuc Hanh khong hop le");
                return;
            }
            else if (monhoc_Data.kiemtra_MaMonHoc(txtMaMonHoc.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ma Mon Hoc");
                txtMaMonHoc.Focus();
                return;
            }
            else if (monhoc_Data.kiemtra_TenMonHoc(txtTenMonHoc.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ten Mon Hoc");
                txtTenMonHoc.Focus();
                return;
            }
            else
            {
                monhoc.maMH = txtMaMonHoc.Text.ToString();
                monhoc.tenMH = txtTenMonHoc.Text.ToString();
                monhoc.tclt = (float)nmTCLT.Value;
                monhoc.tcth = (float)nmTCTH.Value;

                monhoc.them_MonHoc(monhoc);
                monhoc.hienthi_MonHoc(dtgMonHoc);
            }
            txtMaMonHoc.Text = "MH";
            txtTenMonHoc.Text = "";
            nmTCLT.Value = 0;
            nmTCTH.Value = 0;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaMonHoc.Text == "")
            {
                txtMaMonHoc.Focus();
                MessageBox.Show("Ban chua dien Ma Mon Hoc");
                return;
            }
            else if (txtTenMonHoc.Text == "")
            {
                txtTenMonHoc.Focus();
                MessageBox.Show("Ban chua dien Ten Mon Hoc");
                return;
            }
            else if (nmTCLT.Value <= 0)
            {
                MessageBox.Show("Tin Chi Ly Thuyet khong hop le");
                return;
            }
            else if (nmTCTH.Value<= 0)
            {
                MessageBox.Show("Tin Chi Thuc Hanh khong hop le");
                return;
            }
            else
            {
                monhoc.maMH = txtMaMonHoc.Text.ToString();
                monhoc.tenMH = txtTenMonHoc.Text.ToString();
                monhoc.tclt = (float)nmTCLT.Value;
                monhoc.tcth = (float)nmTCTH.Value;

                monhoc.xoa_MonHoc(txtMaMonHoc.Text.ToString());
                monhoc.hienthi_MonHoc(dtgMonHoc);
            }
            txtMaMonHoc.Text = "MH";
            txtTenMonHoc.Text = "";
            nmTCLT.Value = 0;
            nmTCTH.Value = 0;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaMonHoc.Text == "")
            {
                txtMaMonHoc.Focus();
                MessageBox.Show("Ban chua dien ma Mon Hoc");
                return;
            }
            else if (monhoc_Data.kiemtraMaMH_GiangDay(txtMaMonHoc.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa Mon Hoc trong Giang Day");
                return;
            }
            else
            {
                monhoc.xoa_MonHoc(txtMaMonHoc.Text.ToString());
                monhoc.hienthi_MonHoc(dtgMonHoc);
                txtMaMonHoc.Text = "MH";
                txtTenMonHoc.Text = "";
                nmTCLT.Value = 0;
                nmTCTH.Value = 0;
            }
        }

        private void dtgMonHoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgMonHoc.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaMonHoc.Text = dtgMonHoc.Rows[dong].Cells[0].Value.ToString();
            txtTenMonHoc.Text = dtgMonHoc.Rows[dong].Cells[1].Value.ToString();
            nmTCLT.Value = Convert.ToDecimal(dtgMonHoc.Rows[dong].Cells[2].Value.ToString());
            nmTCTH.Value = Convert.ToDecimal(dtgMonHoc.Rows[dong].Cells[3].Value.ToString());
        }

        private void txtMaMonHoc_MouseClick(object sender, MouseEventArgs e)
        {
            txtMaMonHoc.Text = "MH";
            txtTenMonHoc.Text = "";
            nmTCLT.Value = 0;
            nmTCTH.Value = 0;
        }

       

        private void nmTCLT_Leave(object sender, EventArgs e)
        {
            nmTCTH.Value = (nmTCLT.Value) / 2;
        }

        
    }
}
